import*as e from"../layer_viewer/layer_viewer.js";import*as t from"../../ui/legacy/legacy.js";import*as r from"../../core/common/common.js";import*as i from"../../core/i18n/i18n.js";import*as a from"../../core/sdk/sdk.js";class n extends t.SplitWidget.SplitWidget{logTreeView;paintProfilerView;constructor(t){super(!0,!1),this.logTreeView=new e.PaintProfilerView.PaintProfilerCommandLogView,this.setSidebarWidget(this.logTreeView),this.paintProfilerView=new e.PaintProfilerView.PaintProfilerView(t),this.setMainWidget(this.paintProfilerView),this.paintProfilerView.addEventListener(e.PaintProfilerView.Events.WindowChanged,this.onWindowChanged,this),this.logTreeView.focus()}reset(){this.paintProfilerView.setSnapshotAndLog(null,[],null)}profile(e){function t(e,t){this.logTreeView.setCommandLog(t||[]),this.paintProfilerView.setSnapshotAndLog(e,t||[],null),e&&e.release()}e.commandLog().then((r=>t.call(this,e,r)))}setScale(e){this.paintProfilerView.setScale(e)}onWindowChanged(){this.logTreeView.updateWindow(this.paintProfilerView.selectionWindow())}}var s,l=Object.freeze({__proto__:null,LayerPaintProfilerView:n});class o extends a.SDKModel.SDKModel{layerTreeAgent;paintProfilerModel;layerTreeInternal;throttler;enabled;lastPaintRectByLayerId;constructor(e){super(e),this.layerTreeAgent=e.layerTreeAgent(),e.registerLayerTreeDispatcher(new y(this)),this.paintProfilerModel=e.model(a.PaintProfiler.PaintProfilerModel);const t=e.model(a.ResourceTreeModel.ResourceTreeModel);t&&t.addEventListener(a.ResourceTreeModel.Events.PrimaryPageChanged,this.onPrimaryPageChanged,this),this.layerTreeInternal=null,this.throttler=new r.Throttler.Throttler(20)}async disable(){this.enabled&&(this.enabled=!1,await this.layerTreeAgent.invoke_disable())}enable(){this.enabled||(this.enabled=!0,this.forceEnable())}async forceEnable(){this.lastPaintRectByLayerId=new Map,this.layerTreeInternal||(this.layerTreeInternal=new d(this)),await this.layerTreeAgent.invoke_enable()}layerTree(){return this.layerTreeInternal}async layerTreeChanged(e){this.enabled&&this.throttler.schedule(this.innerSetLayers.bind(this,e))}async innerSetLayers(e){const t=this.layerTreeInternal;await t.setLayers(e),this.lastPaintRectByLayerId||(this.lastPaintRectByLayerId=new Map);for(const e of this.lastPaintRectByLayerId.keys()){const r=this.lastPaintRectByLayerId.get(e),i=t.layerById(e);i&&i.setLastPaintRect(r)}this.lastPaintRectByLayerId=new Map,this.dispatchEventToListeners(s.LayerTreeChanged)}layerPainted(e,t){if(!this.enabled)return;const r=this.layerTreeInternal.layerById(e);if(!r)return this.lastPaintRectByLayerId||(this.lastPaintRectByLayerId=new Map),void this.lastPaintRectByLayerId.set(e,t);r.didPaint(t),this.dispatchEventToListeners(s.LayerPainted,r)}onPrimaryPageChanged(){this.layerTreeInternal=null,this.enabled&&this.forceEnable()}}a.SDKModel.SDKModel.register(o,{capabilities:a.Target.Capability.DOM,autostart:!1}),function(e){e.LayerTreeChanged="LayerTreeChanged",e.LayerPainted="LayerPainted"}(s||(s={}));class d extends a.LayerTreeBase.LayerTreeBase{layerTreeModel;constructor(e){super(e.target()),this.layerTreeModel=e}async setLayers(e){if(!e)return void this.innerSetLayers(e);const t=new Set;for(let r=0;r<e.length;++r){const i=e[r].backendNodeId;i&&!this.backendNodeIdToNode().has(i)&&t.add(i)}await this.resolveBackendNodeIds(t),this.innerSetLayers(e)}innerSetLayers(e){if(this.setRoot(null),this.setContentRoot(null),!e)return;let t;const r=this.layersById;this.layersById=new Map;for(let i=0;i<e.length;++i){const a=e[i].layerId;let n=r.get(a);n?n.reset(e[i]):n=new h(this.layerTreeModel,e[i]),this.layersById.set(a,n);const s=e[i].backendNodeId;s&&n.setNode(this.backendNodeIdToNode().get(s)||null),!this.contentRoot()&&n.drawsContent()&&this.setContentRoot(n);const l=n.parentId();if(l){const e=this.layersById.get(l);if(!e)throw new Error(`Missing parent ${l} for layer ${a}`);e.addChild(n)}else t&&console.assert(!1,"Multiple root layers"),t=n}t&&(this.setRoot(t),t.calculateQuad(new WebKitCSSMatrix))}}class h{scrollRectsInternal;quadInternal;childrenInternal;image;parentInternal;layerPayload;layerTreeModel;nodeInternal;lastPaintRectInternal;paintCountInternal;stickyPositionConstraintInternal;constructor(e,t){this.layerTreeModel=e,this.reset(t)}id(){return this.layerPayload.layerId}parentId(){return this.layerPayload.parentLayerId||null}parent(){return this.parentInternal}isRoot(){return!this.parentId()}children(){return this.childrenInternal}addChild(e){const t=e;t.parentInternal&&console.assert(!1,"Child already has a parent"),this.childrenInternal.push(t),t.parentInternal=this}setNode(e){this.nodeInternal=e}node(){return this.nodeInternal||null}nodeForSelfOrAncestor(){let e=this;for(;e;e=e.parentInternal)if(e.nodeInternal)return e.nodeInternal;return null}offsetX(){return this.layerPayload.offsetX}offsetY(){return this.layerPayload.offsetY}width(){return this.layerPayload.width}height(){return this.layerPayload.height}transform(){return this.layerPayload.transform||null}quad(){return this.quadInternal}anchorPoint(){return[this.layerPayload.anchorX||0,this.layerPayload.anchorY||0,this.layerPayload.anchorZ||0]}invisible(){return this.layerPayload.invisible||!1}paintCount(){return this.paintCountInternal||this.layerPayload.paintCount}lastPaintRect(){return this.lastPaintRectInternal||null}setLastPaintRect(e){this.lastPaintRectInternal=e}scrollRects(){return this.scrollRectsInternal}stickyPositionConstraint(){return this.stickyPositionConstraintInternal||null}async requestCompositingReasons(){return(await this.layerTreeModel.layerTreeAgent.invoke_compositingReasons({layerId:this.id()})).compositingReasons||[]}async requestCompositingReasonIds(){return(await this.layerTreeModel.layerTreeAgent.invoke_compositingReasons({layerId:this.id()})).compositingReasonIds||[]}drawsContent(){return this.layerPayload.drawsContent}gpuMemoryUsage(){return this.drawsContent()?this.width()*this.height()*4:0}snapshots(){return[this.layerTreeModel.paintProfilerModel.makeSnapshot(this.id()).then((e=>e?{rect:{x:0,y:0,width:this.width(),height:this.height()},snapshot:e}:null))]}didPaint(e){this.lastPaintRectInternal=e,this.paintCountInternal=this.paintCount()+1,this.image=null}reset(e){this.nodeInternal=null,this.childrenInternal=[],this.parentInternal=null,this.paintCountInternal=0,this.layerPayload=e,this.image=null,this.scrollRectsInternal=this.layerPayload.scrollRects||[],this.stickyPositionConstraintInternal=this.layerPayload.stickyPositionConstraint?new a.LayerTreeBase.StickyPositionConstraint(this.layerTreeModel.layerTree(),this.layerPayload.stickyPositionConstraint):null}matrixFromArray(e){return new WebKitCSSMatrix("matrix3d("+e.map((function(e){return e.toFixed(9)})).join(",")+")")}calculateTransformToViewport(e){let r=(new WebKitCSSMatrix).translate(this.layerPayload.offsetX,this.layerPayload.offsetY);if(this.layerPayload.transform){const e=this.matrixFromArray(this.layerPayload.transform),i=new t.Geometry.Vector(this.layerPayload.width*this.anchorPoint()[0],this.layerPayload.height*this.anchorPoint()[1],this.anchorPoint()[2]),a=t.Geometry.multiplyVectorByMatrixAndNormalize(i,r),n=(new WebKitCSSMatrix).translate(-a.x,-a.y,-a.z);r=n.inverse().multiply(e.multiply(n.multiply(r)))}return r=e.multiply(r),r}createVertexArrayForRect(e,t){return[0,0,0,e,0,0,e,t,0,0,t,0]}calculateQuad(e){const r=this.calculateTransformToViewport(e);this.quadInternal=[];const i=this.createVertexArrayForRect(this.layerPayload.width,this.layerPayload.height);for(let e=0;e<4;++e){const a=t.Geometry.multiplyVectorByMatrixAndNormalize(new t.Geometry.Vector(i[3*e],i[3*e+1],i[3*e+2]),r);this.quadInternal.push(a.x,a.y)}this.childrenInternal.forEach((function(e){e.calculateQuad(r)}))}}class y{layerTreeModel;constructor(e){this.layerTreeModel=e}layerTreeDidChange({layers:e}){this.layerTreeModel.layerTreeChanged(e||null)}layerPainted({layerId:e,clip:t}){this.layerTreeModel.layerPainted(e,t)}}var c=Object.freeze({__proto__:null,LayerTreeModel:o,get Events(){return s},AgentLayerTree:d,AgentLayer:h});const u={details:"Details",profiler:"Profiler"},P=i.i18n.registerUIStrings("panels/layers/LayersPanel.ts",u),w=i.i18n.getLocalizedString.bind(void 0,P);let p;class g extends t.Panel.PanelWithSidebar{model;layerViewHost;layerTreeOutline;rightSplitWidget;layers3DView;tabbedPane;layerDetailsView;paintProfilerView;updateThrottler;layerBeingProfiled;constructor(){super("layers",225),this.model=null,a.TargetManager.TargetManager.instance().observeTargets(this),this.layerViewHost=new e.LayerViewHost.LayerViewHost,this.layerTreeOutline=new e.LayerTreeOutline.LayerTreeOutline(this.layerViewHost),this.layerTreeOutline.addEventListener("PaintProfilerRequested",this.onPaintProfileRequested,this),this.panelSidebarElement().appendChild(this.layerTreeOutline.element),this.setDefaultFocusedElement(this.layerTreeOutline.element),this.rightSplitWidget=new t.SplitWidget.SplitWidget(!1,!0,"layerDetailsSplitViewState"),this.splitWidget().setMainWidget(this.rightSplitWidget),this.layers3DView=new e.Layers3DView.Layers3DView(this.layerViewHost),this.rightSplitWidget.setMainWidget(this.layers3DView),this.layers3DView.addEventListener(e.Layers3DView.Events.PaintProfilerRequested,this.onPaintProfileRequested,this),this.layers3DView.addEventListener(e.Layers3DView.Events.ScaleChanged,this.onScaleChanged,this),this.tabbedPane=new t.TabbedPane.TabbedPane,this.rightSplitWidget.setSidebarWidget(this.tabbedPane),this.layerDetailsView=new e.LayerDetailsView.LayerDetailsView(this.layerViewHost),this.layerDetailsView.addEventListener(e.LayerDetailsView.Events.PaintProfilerRequested,this.onPaintProfileRequested,this),this.tabbedPane.appendTab(f.Details,w(u.details),this.layerDetailsView),this.paintProfilerView=new n(this.showImage.bind(this)),this.tabbedPane.addEventListener(t.TabbedPane.Events.TabClosed,this.onTabClosed,this),this.updateThrottler=new r.Throttler.Throttler(100)}static instance(e){return p&&!e?.forceNew||(p=new g),p}focus(){this.layerTreeOutline.focus()}wasShown(){super.wasShown(),this.model&&this.model.enable()}willHide(){this.model&&this.model.disable(),super.willHide()}targetAdded(e){e===a.TargetManager.TargetManager.instance().primaryPageTarget()&&(this.model=e.model(o),this.model&&(this.model.addEventListener(s.LayerTreeChanged,this.onLayerTreeUpdated,this),this.model.addEventListener(s.LayerPainted,this.onLayerPainted,this),this.isShowing()&&this.model.enable()))}targetRemoved(e){this.model&&this.model.target()===e&&(this.model.removeEventListener(s.LayerTreeChanged,this.onLayerTreeUpdated,this),this.model.removeEventListener(s.LayerPainted,this.onLayerPainted,this),this.model.disable(),this.model=null)}onLayerTreeUpdated(){this.updateThrottler.schedule(this.update.bind(this))}update(){if(this.model){this.layerViewHost.setLayerTree(this.model.layerTree());const e=this.model.target().model(a.ResourceTreeModel.ResourceTreeModel);if(e){const t=e.mainFrame;if(t){const e=t.url;this.element.setAttribute("test-current-url",e)}}}return Promise.resolve()}onLayerPainted({data:e}){if(!this.model)return;const t=this.layerViewHost.selection();t&&t.layer()===e&&this.layerDetailsView.update(),this.layers3DView.updateLayerSnapshot(e)}onPaintProfileRequested({data:e}){this.layers3DView.snapshotForSelection(e).then((t=>{t&&(this.layerBeingProfiled=e.layer(),this.tabbedPane.hasTab(f.Profiler)||this.tabbedPane.appendTab(f.Profiler,w(u.profiler),this.paintProfilerView,void 0,!0,!0),this.tabbedPane.selectTab(f.Profiler),this.paintProfilerView.profile(t.snapshot))}))}onTabClosed(e){e.data.tabId===f.Profiler&&this.layerBeingProfiled&&(this.paintProfilerView.reset(),this.layers3DView.showImageForLayer(this.layerBeingProfiled,void 0),this.layerBeingProfiled=null)}showImage(e){this.layerBeingProfiled&&this.layers3DView.showImageForLayer(this.layerBeingProfiled,e)}onScaleChanged(e){this.paintProfilerView.setScale(e.data)}}const f={Details:"details",Profiler:"profiler"};var T=Object.freeze({__proto__:null,LayersPanel:g,DetailsViewTabs:f});export{l as LayerPaintProfilerView,c as LayerTreeModel,T as LayersPanel};
